package cn.hsc.spider.util;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import cn.hsc.spider.service.impl.HttpDownLoadService;

/**
 * 页面下载工具类
 * @author HuscLove
 * created by 2019-10-14
 */
public class PageDownLoadUtil {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String url = "";
		String content = new HttpDownLoadService().download(url).getContent();
		System.out.println(content);
	}

	public static String getPageContent(String url) {
		//1.通过工厂创建httpclient实例对象
		HttpClientBuilder builder = HttpClients.custom();
		CloseableHttpClient httpClient = builder.build();
		//2.Get请求
		HttpGet httpGet = new HttpGet(url);
		try {
			CloseableHttpResponse response = httpClient.execute(httpGet);
			if (response.getStatusLine().getStatusCode() == 200) {
				String entity = EntityUtils.toString(response.getEntity(), "utf-8");
				System.out.println(entity);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return url;
	}
}
